package com.example.superssssss.qqlogin;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;

public class MainActivity extends AppCompatActivity {

    private EditText qqNumber;
    private EditText qqPassword;
    private CheckBox remember;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        qqNumber = (EditText) findViewById(R.id.et_qqNumber);
        qqPassword = (EditText) findViewById(R.id.et_password);
        remember = (CheckBox) findViewById(R.id.cb_remember);
        restoreInfo();
    }

    /**
     * 根据原来保存的文件信息
     * 把保存的账号密码显示到界面
     */
    private void restoreInfo() {
        File file = new File(this.getFilesDir(), "info.txt");
        //文件存在并确有内容就读取
        if (file.exists() && file.length() > 0) {
            try {
                FileInputStream fis = new FileInputStream(file);
                BufferedReader br = new BufferedReader(new InputStreamReader(fis));
                String info = br.readLine();
                String[] strings = info.split("##");
                String qq = strings[0];
                String password = strings[1];
                qqNumber.setText(qq);
                qqPassword.setText(password);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 登录方法
     * @param view
     */
    public void login(View view) {
        String qq = qqNumber.getText().toString().trim();
        String password = qqPassword.getText().toString().trim();

        if (TextUtils.isEmpty(qq) || TextUtils.isEmpty(password)) {
            Toast.makeText(this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }else{
            //记录用户名和密码
            if (remember.isChecked()) {
                try {
                    File file = new File(this.getFilesDir(), "info.txt");
                    FileOutputStream fos = new FileOutputStream(file);
                    String info = qq + "##" + password;
                    fos.write(info.getBytes());
                    fos.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            if ("10000".equals(qq) && "123456".equals(password)) {
                Toast.makeText(this,"登陆成功", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(this,"登录失败", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
